//
//  AddressBookViewController.m
//  ChatApp
//
//  Created by DJL on 15/12/21.
//  Copyright © 2015年 DJL. All rights reserved.
//

#import "AddressBookViewController.h"
#import "EaseMob.h"
#import "NoticeViewController.h"


#define kTableViewCell @"cell"

@interface AddressBookViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong )UITableView *tableView;
@property (nonatomic, strong)UITextField *search;
@property (nonatomic, strong) NSMutableArray *friendArrList;
@end

@implementation AddressBookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"通讯录";
    [self createTableView];
     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem  alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(action:)];
}
- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
    
    [[EaseMob sharedInstance].chatManager asyncFetchBuddyListWithCompletion:^(NSArray *buddyList, EMError *error) {
        if (!error) {
            NSLog(@"获取成功 -- %@",buddyList);
            
            self.friendArrList = [NSMutableArray arrayWithArray:buddyList];
        }
    } onQueue:nil];
    [self.tableView reloadData];
}
- (void)setFriendArrList:(NSMutableArray *)friendArrList {
    _friendArrList = friendArrList;
    [self.tableView  reloadData];
}
- (void)action:(UIBarButtonItem *)bar {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"＋86 中国🇨🇳(手机号)" preferredStyle:UIAlertControllerStyleAlert];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
        // 1111
        textField.placeholder =@"添加好友ID";
        textField.borderStyle = 3;
    }];
    UIAlertAction *action0 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    
    
    }];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"搜索" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
         UITextField *text = alert.textFields.firstObject;
      
        if ([text.text length] == 11) {
  
        UIAlertController *alertFriend = [UIAlertController alertControllerWithTitle:@"提示" message:  [NSString stringWithFormat:@"添加该好友(%@)？",text.text ]preferredStyle:UIAlertControllerStyleAlert];
        
        [alertFriend addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            
            textField.borderStyle = 3;
            textField.placeholder =@"添加备注";
        }];
        UIAlertAction *actionFri = [UIAlertAction actionWithTitle:@"添加" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"添加好友");
             UITextField *text1 = alert.textFields.lastObject;
            
            EMError *error = nil;
            BOOL isSuccess = [[EaseMob sharedInstance].chatManager addBuddy:[NSString stringWithFormat:@"%@",text.text ] message:[NSString stringWithFormat:@"%@",text1.text] error:&error];
            if (isSuccess && !error) {
                NSLog(@"添加成功");
                
            } else {
                /* 创建AlertController对象 */
                
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"添加失败" preferredStyle:UIAlertControllerStyleAlert ];
                /* 创建action对象 */
                
                
                UIAlertAction *actionConfirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
                    
                    
                }];
                /* alert添加action对象 */
                [alert addAction:actionConfirm];
                /* 推出 */
                [self presentViewController:alert animated:YES completion:^{
                    
                    
                }];
                
            }
        }];
        UIAlertAction *actionFri1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
            NSLog(@"取消");
            
        }];
        /* alert添加action对象 */
        [alertFriend addAction:actionFri ];
        [alertFriend addAction:actionFri1 ];
        /* 推出 */
        [self presentViewController:alertFriend animated:YES completion:^{
            
            
        }];
        } else {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"账号为11位手机号" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
               
            }];
            
            [alert addAction:action ];
            /* 推出 */
            [self presentViewController:alert  animated:YES completion:^{
                
                
            }];

            
        }
    }];
    
    /* alert添加action对象 */
    [alert addAction:action0];
    [alert addAction:action ];
    /* 推出 */
    [self presentViewController:alert animated:YES completion:^{
        
        
    }];

}
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewCell];
    
    [self.view addSubview:self.tableView];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    } else if(section == 1){
        
        return 4;
        
    }else {
        
        return self.friendArrList.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCell];
    cell.selectionStyle  = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        self.search = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, self.view.frame.size.width - 60, 40)];
        self.search.borderStyle = 3;
        self.search.placeholder = @"输入好友ID";
        [cell addSubview:_search];
        UIButton *searchBut = [UIButton buttonWithType:UIButtonTypeCustom];
        searchBut.frame= CGRectMake(self.view.frame.size.width - 50, 0, 50, 40);
        [searchBut addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [searchBut setTitle:@"搜索" forState:UIControlStateNormal];
        searchBut .backgroundColor = [UIColor colorWithWhite:0.400 alpha:1.000];
        searchBut.layer.cornerRadius = 5;
        [cell addSubview:searchBut];
        
        
    }else
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.imageView.image = [UIImage imageNamed:@"tianjiahaoyou"];
            cell.textLabel.text = @"申请与通知";
        } else {
            cell.imageView.image = [UIImage imageNamed:@"qunzu"];
            switch (indexPath.row) {
                case 1:
                    cell.textLabel.text = @"群组";
                    break;
                case 2:
                    cell.textLabel.text = @"聊天室列表";
                    break;
                case 3:
                    cell.textLabel.text = @"小助手";
                    break;
                    
                default:
                    break;
            }
        }
    } else {
        
        EMBuddy *embuddy = [self.friendArrList objectAtIndex:indexPath.row];
        cell.imageView.image = [UIImage imageNamed:@"iconfont-logo"];
        cell.textLabel.text = embuddy.username;
    }
    return cell;
}
//搜索方法
- (void)click:(UIButton *)but {
    NSLog(@"haoyouID");
     [self.view endEditing:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
    lable.textAlignment = NSTextAlignmentCenter;
    lable.textColor = [UIColor blackColor];
//    lable.backgroundColor = [UIColor grayColor];
    lable.alpha = 0.7;
    if (section == 0) {
         lable.text = @"好友搜索";
    }else if (section == 1) {
         lable.text = @"消息中心";
    }else {
    lable.text = @"我的好友";
    }
    return lable;
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 1 && indexPath.row == 0) {
        NoticeViewController *noti =  [NoticeViewController shareNoticeViewController];
        
        [self.navigationController pushViewController:noti animated:YES];
    }
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    [self.view endEditing:YES];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
